﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFIDA.U9.AP.Enums;
using UFIDA.U9.Base.Attachment;
using UFIDA.U9.CBO.FI.Enums;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.cust.qjJSDBE;
using UFSoft.UBF.Business;
using UFSoft.UBF.Eventing;
using UFSoft.UBF.Util.DataAccess;

namespace UFIDA.U9.CusDev.QJKJPlugInBE
{
    class ProcessForJSDAtt : IEventSubscriber
    {
        public void Notify(params object[] args)
        {

            if (args == null || args.Length == 0 || !(args[0] is UFSoft.UBF.Business.EntityEvent)) return;
            BusinessEntity.EntityKey key = ((UFSoft.UBF.Business.EntityEvent)args[0]).EntityKey;

            if (key == null || key.GetEntity() == null) return;
            //结算单
            UFIDA.U9.cust.qjJSDBE.JSDEntity jsd = key.GetEntity() as UFIDA.U9.cust.qjJSDBE.JSDEntity;
            if (jsd == null) return;

            #region 结算单附件写入应收单查看
            using(ISession session = Session.Open())
            {
                //引入应收单
                AR.ARBill.ARBillHead ar = AR.ARBill.ARBillHead.Finder.FindByID(jsd.AR.ID);
                //获取结算单附件信息
                Base.Attachment.Attachment.EntityList attlist = Base.Attachment.Attachment.Finder.FindAll($"EntityID='{jsd.ID}' and DescFlexField.PrivateDescSeg1 ==''");
                if (attlist == null||attlist.Count==0) return;
                foreach (var att in attlist)
                {
                    Base.Attachment.Attachment att1 = Base.Attachment.Attachment.Create();
                    att1.AttachmentID = att.AttachmentID;
                    att1.EntityID = jsd.AR.ID;
                    att1.EntityFullName = "UFIDA.U9.AR.ARBill.ARBillHead";
                    att1.URI = att.URI;
                    att1.Title = att.Title;
                    att1.Size = att.Size;
                    att1.FileHandler = att.FileHandler;
                    att1.AttachType = AccessoryTypeEnum.FileType;                
                    att.DescFlexField.PrivateDescSeg1 = "true";
                }
               

                session.Commit();
            }
           




            #endregion

        }

    }
}
